package utils

import (
	"fmt"
	"os/exec"
)

func AdbPush(file, adbAddr, dist string) error {
	cmd := exec.Command("adb", "-s", adbAddr, "push", file, dist)

	output, err := cmd.CombinedOutput()
	if err != nil {
		return fmt.Errorf("adb push failed: %v, output: %s", err, string(output))
	}
	fmt.Println(string(output))
	//err = SyncMedia(adbAddr, dist)
	//if err != nil {
	//	klog.Errorf("sync media failed:%s", err.Error())
	//}
	return nil
}

func SyncMedia(adbAddr, dist string) error {
	cmd := exec.Command("adb", "-s", adbAddr, "am", "broadcast", "-a", "android.intent.action.MEDIA_SCANNER_SCAN_DIR", "-d", "file://"+dist)

	output, err := cmd.CombinedOutput()
	if err != nil {
		return fmt.Errorf("adb sync media failed: %v, output: %s", err, string(output))
	}
	fmt.Println(string(output))
	return nil
}
